package com.example.springboot.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.ResultCode;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.dao.AdminMapper;
import com.example.springboot.dao.GradeMapper;
import com.example.springboot.dao.NewsMapper;
import com.example.springboot.domain.po.Account;
import com.example.springboot.domain.po.Admin;
import com.example.springboot.domain.po.Grade;
import com.example.springboot.domain.po.News;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.service.Impl
 * @Project：springboot
 * @name：NewsServiceImpl
 * @Date：2025/2/4 21:13
 * @Filename：NewsServiceImpl
 * @for:
 */
@Service
@Slf4j
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Resource
    private NewsMapper newsMapper;

    @Override
    public List<News> getAll() {
        List<News> list = lambdaQuery().list();
        return list;
    }

    @Override
    public News add(News news) {
        String title = news.getTitle();
        news.setTime(DateUtil.now()); //设置当前时间为发布时间
        if (ObjectUtil.isEmpty(title)){
            throw new CustomException(ResultCode.TITLE_ISNULL);
        }
        if (ObjectUtil.isEmpty(news.getImg())) {
            news.setImg("http://localhost:8081/files/1738919020899-微信图片_20240629094510.jpg");
        }

        newsMapper.insert(news);
        return news;
    }

    @Override
    public News findById(Integer id) {
        return newsMapper.selectById(id);
    }




    @Override
    public void delete(Integer id) {
        newsMapper.deleteById(id);
    }


    @Override
    public PageInfo<News> findPage(News search, Integer pageNum, Integer pageSize) {
        Account user = JwtTokenUtils.getCurrentUser();
        if (ObjectUtil.isEmpty(user)) {
            throw new CustomException(ResultCode.USER_NOT_LOGIN);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<News> all = findByCondition(search);

        return PageInfo.of(all);
    }

    @Override
    public List<News> findByCondition(News search) {
        return this.findBySearch(search);
    }




    @Override
    public News newsEdit(News news) {

        String title = news.getTitle();
//     1.先校验标题是不是为空： 如果用户名，为空，抛出异常
        if (ObjectUtil.isEmpty(title)){
            throw new CustomException(ResultCode.TITLE_ISNULL);
        }
        this.update(news);

        return news;
    }
    @Override
    public List<News> GetNewData() {
        return this.getNewdata();
    }
    //@Select("select * from news order by time desc limit 8")
    //List<News> getNewdata();

    public List<News> getNewdata(){
        List<News> list = this.lambdaQuery()
                .orderByDesc(News::getTime)
                .last("limit 8").list();
        return list;
    }

   public void update(News news){
       this.saveOrUpdate(news);
   }

    private List<News> findBySearch(News search) {
        QueryWrapper<News> wrapper = new QueryWrapper<News>()
                .like(search != null && ObjectUtil.isNotEmpty(search.getTitle()), "title", search.getTitle());
        List<News> list = newsMapper.selectList(wrapper);
        return list;
    }
}
